//stc8H2K08U  LCDʾ


#include <COMM/STC8H.h>					 //MCUͷļ
#include "intrins.h"					 //nopָͷļ 
#include "stdio.h"
#define uchar unsigned char                                 //ͺ궨
#define uint unsigned int                                 //ͺ궨


//TM1621d׼1/3

/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_׼1/3*/
#define nop _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); //궨
/******************TM1621Dģ*********************/
#define SYSDIS   0x00    //ϵͳLCDƫѹ        
#define SYSEN    0x02    //ϵͳ      
#define LCDOFF   0x04    //LCDƫѹ
#define LCDON    0x06    //LCDƫѹ                                                         
#define RC       0x30    //ڲRC      
#define BIAS     0x52    //1/3ƫѹ 4       
#define WDTDIS   0x0a    //ֹŹ
#define WDTEN    0x0e    //Ź
/*ڲLCDƫѹҲʾʾLCD ؿŹ ʺ//1/3ƫѹ 4  */

/********************ƶ˿Ҫʵ**********************/
sbit CS=P3^7;
sbit WRITE=P3^6;
sbit DATA=P3^5;

/********************˵ûõTM1621Dٷ*************************/
uchar code Smg[16]={0x05,0xff,0xc7,0xE5,0x6C,0xAD,0xaf,0xE0,0xef,0xed,0xee,0x2f,0x8b,0x67,0x8f,0x8e};  //0~F
uchar code Tab0[16]={0x08,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //
/********************ʱ*************************/
void delay_nms(uint n)
{
   uint i;
   while(n--)
     for(i=0;i<500;i++);
}
/********************Ӹλд*************************/
void Write_Data_H(uchar Data, uchar Cnt)           //DataĸcntλдTM1621Dλǰ
{
uchar i;
for(i=0;i<Cnt;i++)
{
  WRITE=0;
	
  if(Data&0x80)                                   //λ
    DATA=1;               
  else
    DATA=0;
  nop;

  WRITE=1;
  nop;
  Data<<=1;
}
WRITE=0;
DATA=0;
}
/********************ӵλд*************************/
void Write_Data_L(uchar Data,uchar Cnt) //Data ĵcntλдTM1621Dλǰ
{
unsigned char i;
for(i=0;i<Cnt;i++)
{
   WRITE=0;
	        nop;
   if(Data&0x01)                                 //ӵλ
     DATA=1;  
   else
     DATA=0;
        nop;

        WRITE=1;
	         nop;
        Data>>=1;
}
  WRITE=0;
  DATA=0;  
}

/********************д*************************/
void WriteCmd(uchar Cmd)
{
CS=0;
nop;
Write_Data_H(0x80,4);     //д־100
Write_Data_H(Cmd,8);      //д
CS=1;
nop;
}

/*********ַָдݣʵд4λ************/
void WriteOneData(uchar Addr, uchar Data)
{
	
Addr=Addr<<2;
CS=0;
nop;
Write_Data_H(0xa0,3);     //дݱ־101
	
Write_Data_H(Addr,6);  //дַ
Write_Data_L(Data,4);     //д
nop;	
CS=1;
nop;
}

/*********д뷽ʽÿΪ8λдݣÿȥ************/
/*
void WriteAllData(uchar Addr,uchar *p,uchar cnt)
{
uchar i;
CS=0;
Write_Data_H(0xa0,3);          //дݱ־101
Write_Data_H(Addr<<2,6);        //дַ
for(i=0;i<cnt;i++)                //д
{
   Write_Data_L(*p,8);            
   p++;
}
CS=1;
nop;
}
*/
/*******************TM1621Dʼ**********************/
void TM1621D_init()
{
CS=1;
WRITE=1;
DATA=1;
nop;
delay_nms(1);                        
WriteCmd(BIAS);                 //1/3ƫѹ 4
WriteCmd(RC);                         //ڲRC	
WriteCmd(SYSDIS);                 //ϵͳLCDƫѹ
WriteCmd(WDTDIS);								 //ֹŹ
WriteCmd(SYSEN);                 //ϵͳ
WriteCmd(LCDON);                 //LCDƫѹ
}

/*************TM16211621DӦȫղǶԵ*****************/
void Clear1621()
{
uchar i;
for(i=0;i<32;i++) //9-21
{
WriteOneData(i, 0x00);
}
}
/*************TM1621ȫ ûȫʾвԵĵطҪӵ*****************/
/*
void quanping1621()
{
uchar i;
for(i=9;i<22;i++) //9-21
{
WriteOneData(i, 0x0f);
}
}
*/
/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_׼1/3*/

//TM1621d׼1/3

//ʾlcd_xsbl
unsigned int lcd_xsbl=0;//lcdʾ
static void lcd_1(unsigned long xsbl_lcd)//1λλ(λ)
{
				 if(xsbl_lcd / 100 == 0)
    {
        WriteOneData(0x09,0x0b);	WriteOneData(0x0e,0x0e);  	            // 0
    }
    else if(xsbl_lcd / 100== 1)
    {
        WriteOneData(0x09,0x00);	WriteOneData(0x0e,0x06);  	            // 1
    }
    else if(xsbl_lcd / 100== 2)
    {
        WriteOneData(0x09,0x07);	WriteOneData(0x0e,0x0c);  	            // 2
    }   
    else if(xsbl_lcd / 100== 3)
    {
        WriteOneData(0x09,0x05);	WriteOneData(0x0e,0x0e);  	            // 3
    } 
    else if(xsbl_lcd / 100== 4)
    {
        WriteOneData(0x09,0x0c);	WriteOneData(0x0e,0x06);  	            // 4
    }
    else if(xsbl_lcd / 100== 5)
    {
        WriteOneData(0x09,0x0d);	WriteOneData(0x0e,0x0a);  	            // 5
    }
    else if(xsbl_lcd / 100== 6)
    {
        WriteOneData(0x09,0x0f);	WriteOneData(0x0e,0x0a);  	            // 6
    }
    else if(xsbl_lcd / 100== 7)
    {
        WriteOneData(0x09,0x00);	WriteOneData(0x0e,0x0e);                // 7
    }
    else if(xsbl_lcd / 100== 8)
    {
        WriteOneData(0x09,0x0f);	WriteOneData(0x0e,0x0e);  	            // 8
    }
    else if(xsbl_lcd / 100== 9)
    {
				WriteOneData(0x09,0x0d);	WriteOneData(0x0e,0x0e); 	      	      // 9
    }
}

static void lcd_2(unsigned long xsbl_lcd)//2λʮλ(λ)
{
				 if(xsbl_lcd % 100/10== 0)
    {
        WriteOneData(0x0c,0x0e);	WriteOneData(0x0d,0x0b);  	            // 0
    }
    else if(xsbl_lcd % 100/10== 1)
    {
        WriteOneData(0x0c,0x06);	WriteOneData(0x0d,0x00);  	            // 1
    }
    else if(xsbl_lcd % 100/10== 2)
    {
        WriteOneData(0x0c,0x0c);	WriteOneData(0x0d,0x07);  	            // 2
    }   
    else if(xsbl_lcd % 100/10== 3)
    {
        WriteOneData(0x0c,0x0e);	WriteOneData(0x0d,0x05);  	            // 3
    } 
    else if(xsbl_lcd % 100/10== 4)
    {
        WriteOneData(0x0c,0x06);	WriteOneData(0x0d,0x0c);  	            // 4
    }
    else if(xsbl_lcd % 100/10== 5)
    {
        WriteOneData(0x0c,0x0a);	WriteOneData(0x0d,0x0d);  	            // 5
    }
    else if(xsbl_lcd % 100/10== 6)
    {
        WriteOneData(0x0c,0x0a);	WriteOneData(0x0d,0x0f);  	            // 6
    }
    else if(xsbl_lcd % 100/10== 7)
    {
        WriteOneData(0x0c,0x0e);	WriteOneData(0x0d,0x00);  	            // 7
    }
    else if(xsbl_lcd % 100/10== 8)
    {
        WriteOneData(0x0c,0x0e);	WriteOneData(0x0d,0x0f);  	            // 8
    }
    else if(xsbl_lcd % 100/10== 9)
    {
        WriteOneData(0x0c,0x0e);	WriteOneData(0x0d,0x0d);  	            // 0
    }
}

static void lcd_3(unsigned long xsbl_lcd)//3λλ(λ)
{
    if(xsbl_lcd % 10 == 0)
    {
        WriteOneData(0x0a,0x0e);	WriteOneData(0x0b,0x0b);  	            // 0
    }
    else if(xsbl_lcd % 10 == 1)
    {
        WriteOneData(0x0a,0x06);	WriteOneData(0x0b,0x00);  	            // 1
    }
    else if(xsbl_lcd % 10 == 2)
    {
        WriteOneData(0x0a,0x0c);	WriteOneData(0x0b,0x07);  	            // 2
    }   
    else if(xsbl_lcd % 10 == 3)
    {
        WriteOneData(0x0a,0x0e);	WriteOneData(0x0b,0x05);  	            // 3
    } 
    else if(xsbl_lcd % 10 == 4)
    {
        WriteOneData(0x0a,0x06);	WriteOneData(0x0b,0x0c);  	            // 4
    }
    else if(xsbl_lcd % 10 == 5)
    {
        WriteOneData(0x0a,0x0a);	WriteOneData(0x0b,0x0d);  	            // 5
    }
    else if(xsbl_lcd % 10 == 6)
    {
        WriteOneData(0x0a,0x0a);	WriteOneData(0x0b,0x0f);  	            // 6
    }
    else if(xsbl_lcd % 10 == 7)
    {
        WriteOneData(0x0a,0x0e);	WriteOneData(0x0b,0x00);  	            // 7
    }
    else if(xsbl_lcd % 10 == 8)
    {
        WriteOneData(0x0a,0x0e);	WriteOneData(0x0b,0x0f);  	            // 8
    }
    else if(xsbl_lcd % 10 == 9)
    {
        WriteOneData(0x0a,0x0e);	WriteOneData(0x0b,0x0d);  	            // 0
    }
}
	
//ʾlcd_xsbl


void Delay1000ms(void)	//@11.0592MHz
{
unsigned char data i, j, k;

	i = 57;
	j = 27;
	k = 112;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);

}

//////////////////////////////////////////////////////////////////ʪģ1

	
typedef unsigned char   uint8;	//  8 bits 
typedef unsigned int  	uint16;	// 16 bits 
typedef unsigned long   uint32;	// 32 bits 

typedef unsigned char   uint8_t;	//  8 bits 
typedef unsigned int  	uint16_t;	// 16 bits 
typedef unsigned long   uint32_t;	// 32 bits 

typedef signed char     int8;	//  8 bits 
typedef signed int      int16;	// 16 bits 
typedef signed long     int32;	// 32 bits 



float HUM = 0;
float HUM_TEMP = 0;
float TEMP = 0;
float TEMP_TEMP = 0;
uint8 SENDAC[3]={0,0,0};
uint8 RES[6]={0x00,0x01,0x02,0x03,0x04,0x05};
uint32 RES1 = 0;
uint32 RES2 = 0;
uint32 RES3 = 0;
uint32 RES4 = 0;
uint32 RES5 = 0;


sbit SDA=P1^0;  
sbit SCL=P1^1;


//IICԤȶ






//IIC¶


void softiic_init();	//IICʼ
void softiic_start();	//IICstartź
void softiic_stop();	//IICstopź
uint8 softiic_wait_ack();//IICȴackź
void softiic_ack();	//IICackź
void softiic_nack();	//IICNAckź
void softiic_send_byte(uint8 IIC_Byte);	//IICһֽ
uint8 softiic_read_byte();	//IICȡһֽ
void softiic_read_nbyte(uint8 device_addr, uint8 reg_addr, uint8 *p, uint8 number);	//IICȡnֽ
void softiic_write_nbyte(uint8 device_addr, uint8 reg_addr, uint8 *p, uint8 number);	//IICдnֽ





void delay1us(void)	//@11.0592MHz
{
	unsigned char data i;

	_nop_();
	_nop_();
	i = 1;
	while (--i);
}



//========================================================================
// : ʱģڿɵã.
// : ʱusȷģ.
// : none.
//========================================================================
static void delay_us(uint8 time)
{
    do { delay1us();} while (--time);
}

//========================================================================
// : IICʼ.
// : none.
// : none.
//========================================================================
void softiic_init()
{
    P1M0 |= 0x02; P1M1 |= 0x02; //SCL©;//©
    P1M0|=0x01;P1M1|=0x01; //SDA©;//©
    SDA = 1;
    SCL = 1;
}

//========================================================================
// : IICstartź.
// : none.
// : none.
//========================================================================
void softiic_start()
{
	P1M0|=0x01;P1M1|=0x01; //SDA©;      //sda
	SDA=1;	  	  
	SCL=1;
	delay_us(4);
 	SDA=0;      //CLKΪߵƽʱSDAһ½شһʼź
	delay_us(4);
	SCL=0;      //ǶסIICߣ׼ͻ߽
}

//========================================================================
// : IICstopź.
// : none.
// : none.
//========================================================================
void softiic_stop()
{
	P1M0|=0x01;P1M1|=0x01; //SDA©;      //sda
	SCL=0;
	SDA=0;      //CLKΪߵƽʱSDAһشһʼź
 	delay_us(4);
	SCL=1; 
	SDA=1;      //ͽź
	delay_us(4);	
}

//========================================================================
// : IICȴackź.
// : none.
// : 0յӦź; 1ûнյӦź.
//========================================================================
uint8 softiic_wait_ack()
{
	uint8 ucErrTime=0;
	P1M1|=0x01;P1M0&=~0x01;//P1_0 ;      //SDAΪ
	SDA=1;delay_us(1);	   
	SCL=1;delay_us(1);	 
	while(SDA == 1)
	{
		ucErrTime++;
		if(ucErrTime>250)
		{
			softiic_stop();
			return 1;
		}
	}
	SCL=0;//ʱ0 	   
	return 0; 
}

//========================================================================
// : IICackź.
// : none.
// : none.
//========================================================================
void softiic_ack()
{
	SCL=0;
	P1M0|=0x01;P1M1|=0x01; //SDA©;
	SDA=0;
	delay_us(2);
	SCL=1;
	delay_us(2);
	SCL=0;
}

//========================================================================
// : IICNAckź.
// : none.
// : none.
//========================================================================
void softiic_nack()
{
	SCL=0;
	P1M0|=0x01;P1M1|=0x01; //SDA©;
	SDA=1;
	delay_us(2);
	SCL=1;
	delay_us(2);
	SCL=0;
}

//========================================================================
// : IICһֽ.
// : ͵ֽ.
// : none.
//========================================================================
void softiic_send_byte(uint8 IIC_Byte)
{
    uint8 t;   
	P1M0|=0x01;P1M1|=0x01; //SDA©; 	    
    SCL=0;      //ʱӿʼݴ
    for(t=0;t<8;t++)
    {              
        SDA=(IIC_Byte&0x80)>>7;
        IIC_Byte<<=1; 	  
		delay_us(2);
		SCL=1;
		delay_us(2); 
		SCL=0;	
		delay_us(2);
    }	
}

//========================================================================
// : IICȡһֽ.
// : none
// : ȡֽ.
//========================================================================
uint8 softiic_read_byte()
{
	uint8 i,receive=0;
	P1M1|=0x01;P1M0&=~0x01;//P1_0 ;   //SDAΪ
    for(i=0;i<8;i++ )
	{
        SCL=0; 
        delay_us(2);
		SCL=1;
        receive<<=1;
        if(SDA == 1)receive++;   
		delay_us(1); 
    }					 
    return receive;
}

//========================================================================
// : IICȡnֽ.
// : 豸ַ8λģʽ7λַҪһλĴַݵַ
// : none.
//========================================================================
void softiic_read_nbyte(uint8 device_addr, uint8 reg_addr, uint8 *p, uint8 number)
{
    softiic_start();                        //ʼź
    softiic_send_byte(device_addr);      //дַ
    softiic_wait_ack();
    softiic_send_byte(reg_addr);                //ͼĴַ
    softiic_wait_ack();

    softiic_start();                        //ظʼź
    softiic_send_byte(device_addr | 0x01);      //ַ
    softiic_wait_ack();	
    do
    {
        *p = softiic_read_byte();
        p++;
        if(number != 1) softiic_ack();     

    }while(--number);
    softiic_nack(); 
    softiic_stop();
}

//========================================================================
// : IICдnֽ.
// : 豸ַ8λģʽ7λַҪһλĴַݵַ
// : none.
//========================================================================
void softiic_write_nbyte(uint8 device_addr, uint8 reg_addr, uint8 *p, uint8 number)
{
    softiic_start();                        //ʼź
    softiic_send_byte(device_addr);      //дַ
    softiic_wait_ack();
    softiic_send_byte(reg_addr);                //ͼĴַ
    softiic_wait_ack();
    do
    {
        softiic_send_byte(*p++);             //
        softiic_wait_ack();
    }while(--number);
    softiic_stop();                          //ֹͣ
}





//////////////////////////////////////////////////////////////////ʪģ1



















 
 
 

void main()
{
//////////////////////////////////////////////////////////////////ʪģ2

//////////////////////////////////////////////////////////////////ʪģ2
//main
//TM1621dӦò2/3
	{
/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_Ӧò2/3*/
    P3M0 = 0x00; P3M1 = 0x00; 
		P3M0 &= ~0x09; P3M1 &= ~0x09; //P33Ϊߵƽ

P33=1;


TM1621D_init();                //ʼظʹѭҪĻ
delay_nms(1);
	Clear1621();//LCDԴ
	
		
		

		
		
//quanping1621();  //ӦÿȫʾŶ
/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_Ӧò2/3*/
	}
	

//////////////////////////////////////////////////////////////////ʪģ3
	
	
  P1M1=0x00;P1M0=0x00;//˫IO
  softiic_init();
	
	
	
	
//////////////////////////////////////////////////////////////////ʪģ3
while(1)
{//while
	Delay1000ms();

	
softiic_write_nbyte(0x70,0xAC,SENDAC,2);//I2Cдnֽ
Delay1000ms();
  softiic_read_nbyte(0X71,0X00,RES,6);//I2Cȡnֽ
  RES1 = RES[(int)(1)];
  RES2 = RES[(int)(2)];
  RES3 = RES[(int)(3)];
  RES4 = RES[(int)(4)];
  RES5 = RES[(int)(5)];
  HUM = ((RES1<<12) + (RES2<<4)) + (RES3>>4);
  TEMP = (0xfffff&(((RES3<<16) + (RES4<<8)) + RES5));
  HUM_TEMP = (HUM * 100) / 0X100000;
  TEMP_TEMP = (TEMP * 200) / 0X100000 - 50;


	


	Delay1000ms();	

lcd_1(TEMP_TEMP);
lcd_2(TEMP_TEMP);
lcd_3(TEMP_TEMP);
	Delay1000ms();
//ʾ¶ˡ
/*
	lcd_xsbl=lcd_xsbl+1;
	
	if (lcd_xsbl>=999)
		lcd_xsbl=0;
	

	*/
/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_ʾ3/3*/
//ceshi=ceshi+11;

/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_ʾ3/3*/
	
}//while
	
}